﻿using System;
using System.IO;
using System.Windows.Forms;

namespace DownloadPhimBongngo
{
    public partial class MainForm : Form
    {
        private const string _setting = "Setting.txt";
        private string _filePath = "";

        public MainForm()
        {
            InitializeComponent();

            backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
            backgroundWorker1.DoWork += backgroundWorker1_DoWork;
            backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;

            Load += MainForm_Load;
            FormClosed += MainForm_FormClosed;

            lblMsg.Text = "";
        }

        void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            progressBar1.Style = ProgressBarStyle.Blocks;
            lblMsg.Text = "Download Completed.";
        }

        void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            lblMsg.Text = e.ProgressPercentage.ToString();
        }

        void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            var downloader = new Downloader();
            downloader.progressChangedEvent += downloader_progressChangedEvent;
            downloader.Download(txtUrl.Text, _filePath);
            downloader.progressChangedEvent -= downloader_progressChangedEvent;
        }

        void downloader_progressChangedEvent(int downloadedFileCount)
        {
            backgroundWorker1.ReportProgress(downloadedFileCount);
        }

        void MainForm_Load(object sender, EventArgs e)
        {
            txtFolderPath.Text = File.ReadAllText(_setting);

            if (Directory.Exists(txtFolderPath.Text) == false)
                Directory.CreateDirectory(txtFolderPath.Text);

            folderBrowserDialog1.SelectedPath = txtFolderPath.Text;
        }

        void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            File.WriteAllText(_setting, txtFolderPath.Text);
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFolderPath.Text))
            {
                MessageBox.Show(this, "Please select Save To folder path", "Save To is empty");
                return;
            }

            if (string.IsNullOrEmpty(txtFileName.Text))
            {
                MessageBox.Show(this, "Please enter file name", "File name is empty");
                return;
            }

            try
            {
                _filePath = Path.Combine(txtFolderPath.Text, txtFileName.Text, ".ts");
                File.Create(_filePath).Close();
                File.Delete(_filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "A file name cannot contain any of the following characters:\n     \\ / : * ? \" < > |", "File name invalid");
                return;
            }

            progressBar1.Style = ProgressBarStyle.Marquee;
            backgroundWorker1.RunWorkerAsync();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
            {
                txtFolderPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}
